var time = require('../../../../utils/util');
const util = require('../../../../utils/util')
var BASE_URL = getApp().globalData.BASE_URL


Page({

  /**
   * 页面的初始数据
   */
  data: {
    showfinish: false, //控制没有数据的出现和隐藏
    show: false, //控制专业设置和攻略分享的出现和隐藏
    showflag: false, //控制上拉加载更多的出现和隐藏
    active: 0, //控制tabs的默认选择
    id: null, //从别的页面传过来的id
    school: '', //学校信息
    otherschool: '', //学校申请要求
    imglist: null, //预览的图片列表
    _num: 0, //点击专业详情  控制变蓝
    showloading: true, //首次加载页面 加载后隐藏
    xueli: "", //专业设置  后台返回的课程
    major: '', //专业列表
    flag: '', //点击专业详情  控制变蓝
    askList: '', //在线问答  问题列表
    gonglueList: '', //攻略分享 文章列表
    current_page: '', //上拉加载更多当前页
    last_page: '', //上拉加载更多最后页
    cate: '', //tabs切换tab,
    majordetail: '',
    showmajordetail: false,
    showmajorload: true,
    xueliid:'',
    majorid:''
  },
  onLoad: function (options) {
    // options.id 就是首页传过来的id，接下来循环找到id所匹配的数据就可以进行渲染啦！
    console.log(options)
    this.setData({
      id: options.id
    })
    if (options.xueliid) {
      this.setData({
        active: 'sz',
        cate:'sz',
        xueliid:options.xueliid,
        majorid:options.majorid
      })
      console.log(this.data.xueli,options.xueliid)
    }
  },

  onReady: function () {
    var _this = this;
    wx.request({
      url: `${BASE_URL}/school/detail/id/${_this.data.id}.html`,
      method: "POST",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        res.data.list.logo = `${BASE_URL}/uploads/school/` + res.data.list.logo
        var pic_list = []
        res.data.list.img.split(",").forEach(function (item, index) {
          var str = {
            src: `${BASE_URL}/uploads/school/xuexiaotupian/` + item,
            id: index
          }
          pic_list.push(str)
        })
        _this.setData({
          school: res.data.list,
          otherschool: res.data.apply,
          imglist: pic_list,
          showloading: false
        })
        console.log(res)
      }
    })
  },

  onChange(event) {
    console.log(event)
    this.setData({
      cate: event.detail.name,
      show: false,
    })
    var _this = this;
    if (event.detail.name == 'sz') {
      if (_this.data.xueli == '') {
        this.setData({
          show: true
        })
      }
      wx.request({
        url: `${BASE_URL}/school/detail/id/${_this.data.id}/type/${event.detail.name}.html`,
        method: "POST",
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          res.data.set_sub.forEach(function (item, index) {
            item.forEach(function (item, index) {
              if (_this.data.majorid == item.id && _this.data.majorid!=='' ) {
                _this.setData({
                  majordetail: item.neirong,
                  showmajorload: false,
                  flag:_this.data.majorid,
                  showmajordetail:true
                })
              } else {
                item.neirong = ''
              }
            })
          })
          res.data.set.forEach(function (item, index) {
            if(item.id==_this.data.xueliid){
              console.log(2132138712836812763,index)
              _this.setData({
                _num: index,
              })
            }
          })
          console.log(2132138712836812763,_this.data._num)
          var arr=[]
          if(_this.data.xueliid!==''&&_this.data.majorid!==''){
          res.data.set_sub[_this.data._num].forEach(function (item, index) {
            if (_this.data.majorid == item.id && _this.data.majorid!=='' ) {
            arr.unshift(item)
            } else {
             arr.push(item)
            }
          })
          res.data.set_sub[_this.data._num]=arr
        }
          _this.setData({
            xueli: res.data.set,
            major: res.data.set_sub,
            show: false
          })
          console.log(res)

        }
      })
    }
    if (event.detail.name == 'wd') {
      if (_this.data.askList == '') {
        _this.setData({
          show: true
        })
      }
      wx.request({
        url: `${BASE_URL}/school/detail/id/${_this.data.id}/type/${event.detail.name}.html`,
        method: "POST",
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          res.data.ask.data.forEach(function (item, index) {
            item.time = time.formatTimeTwo(item.time, 'Y-M-D h:m:s');
          })
          console.log(res)
          _this.setData({
            askList: res.data.ask.data,
            show: false
          })
        }
      })
    }
    if (event.detail.name == 'fx') {
      if (_this.data.gonglueList == '') {
        _this.setData({
          show: true
        })
      }
      wx.request({
        url: `${BASE_URL}/school/detail/id/${_this.data.id}/type/${event.detail.name}.html`,
        method: "POST",
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          res.data.fenxiang.data.forEach(function (item, index) {
            item.time = time.formatTimeTwo(item.time, 'Y-M-D h:m:s');
          })
          console.log(res)
          _this.setData({
            gonglueList: res.data.fenxiang.data,
            show: false,
            current_page: res.data.fenxiang.current_page,
            last_page: res.data.fenxiang.last_page
          })
        }
      })
    }
  },
  //专业详情
  bindtapactive: function (e) {
    this.setData({
      flag: e.currentTarget.dataset.id,
      showmajordetail: !this.data.showmajordetail,
      showmajorload: true
    })
    var _this = this
    if (_this.data.showmajordetail) {
      wx.request({
        url: `${BASE_URL}/school/detail/id/${_this.data.id}/type/${_this.data.cate}.html`,
        method: "POST",
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          res.data.set_sub.forEach(function (item, index) {
            item.forEach(function (item, index) {
              if (item.id == e.currentTarget.dataset.id) {
                _this.setData({
                  majordetail: item.neirong,
                  showmajorload: false
                })
              } else {
                item.neirong = ''
              }
            })
          })
        }
      })
    }

  },




  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: util.throttle(function () {
      var _this = this
      if (_this.data.cate == 'fx') {
        if (_this.data.current_page !== _this.data.last_page) {
          _this.setData({
            current_page: _this.data.current_page + 1,
            showflag: true,
            showfinish: false,
          })
        } else {
          _this.setData({
            showflag: false,
            showfinish: true,
          })
        }
        if (_this.data.showflag) {
          setTimeout(function () {
            wx.request({
              url: `${BASE_URL}/school/detail/id/${_this.data.id}/type/fx.html?page=${_this.data.current_page}`,
              header: {
                'Content-Type': 'application/json', //GET方式是这个
              },
              method: "POST",
              success(res) {
                console.log(res)
                res.data.fenxiang.data.forEach(function (item, index) {
                  item.time = time.formatTimeTwo(item.time, 'Y-M-D h:m:s');
                })
                _this.setData({
                  gonglueList: _this.data.gonglueList.concat(res.data.fenxiang.data),
                  showflag: false,
                })
              }
            })
          }, 1000)
        }
      }
    },
    2000),




  //图片预览
  topic_preview: function (e) {
    var that = this;
    var url = e.currentTarget.dataset.url;
    var previewImgArr = [];
    //通过循环在数据链里面找到和这个id相同的这一组数据，然后再取出这一组数据当中的图片
    that.data.imglist.forEach(function (item, index) {
      var str = item.src
      previewImgArr.push(str)
    })
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: previewImgArr // 需要预览的图片http链接列表
    })
  },

  //跳转文章详情页
  articleDetail: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/views/txtdetail/index?id=${id}`
    })
  },

  //跳转问答详情页
  answerDetail: function (e) {
    var id = e.currentTarget.dataset.id //获取点击产品时拿到的id，就是data-id传过来的值
    // wx.navigateTo跳转页面的方法
    //URL是传递的是详情页的路径，把id拼接传过去就可以啦
    wx.navigateTo({
      url: `/views/answerdetail/index?id=${id}`
    })
  },
})